package ru.bragnikita.utils.swing.widget.form;

import com.google.common.collect.Maps;
import net.engio.mbassy.listener.Handler;
import ru.bragnikita.utils.swing.message.EventUtils;
import ru.bragnikita.utils.swing.message.PanelMessage;
import ru.bragnikita.utils.swing.utils.PanelTest;
import ru.bragnikita.utils.swing.widget.WtButton;
import ru.bragnikita.utils.swing.widget.WtTextField;
import ru.bragnikita.utils.swing.widget.field.Field;

import javax.swing.*;
import java.awt.*;
import java.util.Map;

/**
 * @author Nikita.Brazhnikov
 */
public class FormPanel extends WidgetController {

	private JPanel panel;

	private int col = 0;
	private int row = 0;

	private JPanel currentSubPanel;
	private GridBagLayout currentSubPanelLayout;

	public FormPanel() {
		this(null, null);
	}

	public FormPanel(String name, String panelId) {
		super(panelId);
		panel = new JPanel();
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
		if (name != null) {
			panel.setBorder(BorderFactory.createTitledBorder(name));
		}
	}

	public FormPanel add(Field field) {
		WtTextField widget = new WtTextField(field);
		JLabel label = widget.getLabel();
		JComponent comp = widget.getComponent();
		comp.setMinimumSize(comp.getPreferredSize());
		placeTextField(label, comp);
		register(widget);
		return this;
	}

	public FormPanel addButton(String label, String id) {
		JButton button = new JButton(label);
		WtButton wtButton = new WtButton(id, label, button);
		register(wtButton);
		placeSingleWidget(button);
		return this;
	}

	public FormPanel newCol() {
		col++;
		row = 0;
		return this;
	}

	public FormPanel newRow() {
		row++;
		col = 0;
		return this;
	}

	public FormPanel newGroup() {
		currentSubPanel = createAndAddPanel();
		resetGrid();
		return this;
	}

	public FormPanel newGroup(String name) {
		newGroup();
		currentSubPanel.setBorder(BorderFactory.createTitledBorder(name));
		return this;
	}

	private void placeTextField(JLabel label, JComponent component) {
		final GridBagConstraints c = new GridBagConstraints();
		c.insets = new Insets(3, 5, 3, 5);
		c.anchor = GridBagConstraints.LINE_END;
		c.gridy = row;
		c.gridx = col * 2;
		getCurrentPanel().add(label, c);
		c.gridx++;
		c.anchor = GridBagConstraints.LINE_START;
		getCurrentPanel().add(component, c);
		row++;
	}

	private void placeSingleWidget(JComponent component) {
		final GridBagConstraints c = new GridBagConstraints();
		c.anchor = GridBagConstraints.LINE_START;
		c.gridx = col;
		c.gridy = row;
		getCurrentPanel().add(component, c);
		col++;
	}

	private JPanel getCurrentPanel() {
		if (currentSubPanel == null) {
			return createAndAddPanel();
		}
		return currentSubPanel;
	}

	private GridBagLayout getPanelLayout() {
		return currentSubPanelLayout;
	}

	private JPanel createAndAddPanel() {
		currentSubPanelLayout = new GridBagLayout();
		currentSubPanel = new JPanel(currentSubPanelLayout);
		panel.add(currentSubPanel);
		return currentSubPanel;
	}

	private void resetGrid() {
		col = 0;
		row = 0;
	}

	public JPanel getPanel() {
		return panel;
	}


	public static void main(String[] args) {

		FormPanel formPanel = new FormPanel("Database", "database_panel");
		formPanel.add(new ru.bragnikita.utils.swing.widget.field.TextField("user", "User").withLength(10));
		formPanel.add(new ru.bragnikita.utils.swing.widget.field.TextField("password", "Password").withLength(15));
		formPanel.newCol();
		formPanel.add(new ru.bragnikita.utils.swing.widget.field.TextField("url", "URL").withLength(25));
		formPanel.add(new ru.bragnikita.utils.swing.widget.field.TextField("sid", "SID").withLength(5));
		formPanel.newGroup("Other");

		JPanel sp = formPanel.currentSubPanel;
		GridBagConstraints c = new GridBagConstraints();
		//c.fill = GridBagConstraints.BOTH;
		//c.insets = new Insets(3, 5, 3, 5);
		c.anchor = GridBagConstraints.WEST;
		c.weightx = 0.01;
		JLabel l = new JLabel("SID");
		sp.add(l, c);
		c.gridx = 1;
		c.weightx = 1.;
		c.anchor = GridBagConstraints.WEST;
		JTextField f = new JTextField(10);
		sp.add(f, c);

		//formPanel.add(new ru.bragnikita.utils.swing.widget.field.TextField("port", "Port").withLength(4));

		Map<String, Object> valuesMap = Maps.newTreeMap();
		valuesMap.put("user", "Nikita");
		valuesMap.put("password", "1234");
		valuesMap.put("url", "http://google.ru");
		valuesMap.put("port", "12345");
		valuesMap.put("unknown", "someval");
		valuesMap.put("sid", null);
		formPanel.setValues(valuesMap);

		formPanel.newRow();
		formPanel.addButton("Run", "run");

		EventUtils.registerOnPanelMessagesBus(new TestSubscriber());

		JPanel jp = formPanel.getPanel();
		PanelTest.display(jp);
		ValuesMap paramsMap = formPanel.getValues();
		System.out.println(paramsMap.toString());
	}

	private static class TestSubscriber {
		@Handler
		public void handle(PanelMessage panelMessage) {
			System.out.println(panelMessage.getPanelId());
			System.out.println(panelMessage.getActionSourceId());
			System.out.println(panelMessage.getAction());
			System.out.println(panelMessage.getFormParams());
		}
	}

}
